﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.OracleClient;

namespace BpmHsjqYw
{
    public class OracleDBClass
    {
        /*----全局变量定义------*/

        private OracleConnection conn;
        private OracleCommand comm;
        private string connStr;   /* 数据库联接字符串 */
        private string errInfo = "";

        
        //static log4net.ILog _logEik = log4net.LogManager.GetLogger(typeof(BLLUltimusEik));
        /*----------------------*/

        public OracleDBClass()
        {
            connStr = "";
        }
        public void GetConString(string ConnString)
        {
            connStr = ConnString;
        }
        public OracleDBClass(string ConnString)
        {
            connStr = ConnString;
        }
        /* 数据库操作异常信息 只读属性 */
        public string ErrInfo
        {
            get
            {
                return errInfo;
            }
        }

        /* 数据库链接字符串 */
        public string ConnStr
        {
            get
            {
                return connStr;
            }
            set
            {
                connStr = value;
            }
        }
        private void connDb()
        {
            try
            {
                if (conn == null || conn.State == ConnectionState.Closed)
                {
                    conn = new OracleConnection(connStr);
                    conn.Open();
                }
            }
            catch (Exception e)
            {
                errInfo = e.Message;
                conn.Close();
            }
        }

        /* 数据库链接字符串 */
        public OracleConnection Connection
        {
            get
            {
                return conn;
            }
            set
            {
                conn = value;
            }
        }

        public OracleConnection getConnect()
        {
            OracleConnection Connection = null;
            try
            {
                if (conn == null || conn.State == ConnectionState.Closed)
                {
                    conn = new OracleConnection(connStr);
                    conn.Open();

                    Connection = conn;
                }
            }
            catch (Exception e)
            {
                errInfo = e.Message;
                conn.Close();
            }

            return Connection;
        }

        /* 用于窗体绑定 */
        public DataView GetDataView(string sql)
        {
            connDb();
            OracleDataAdapter dad = new OracleDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            dad.Fill(ds);
            DataView dv = new DataView(ds.Tables[0]);
            return dv;
        }

        public DataView GetDataView(string sql, OracleConnection Connection)
        {
            conn = Connection;
            OracleDataAdapter dad = new OracleDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            dad.Fill(ds);
            DataView dv = new DataView(ds.Tables[0]);
            return dv;
        }
        /* 获取DataSet */
        public DataSet GetDataSet(string sql)
        {
            connDb();
            OracleDataAdapter dad = new OracleDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            dad.Fill(ds);
            return ds;
        }

        public DataSet GetDataSet(string sql, OracleConnection Connection)
        {
            conn = Connection;
            OracleDataAdapter dad = new OracleDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            dad.Fill(ds);
            return ds;
        }

        /* 获取DataTable */
        public DataTable GetDataTable(string sql)
        {
            connDb();
            OracleDataAdapter dad = new OracleDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            dad.Fill(ds);
            DataTable dtTemp = null;
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    dtTemp = ds.Tables[0];
                }
            }
            return dtTemp;
        }

        public DataTable GetDataTable(string sql, OracleConnection Connection)
        {
            conn = Connection;
            OracleDataAdapter dad = new OracleDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            dad.Fill(ds);
            DataTable dtTemp = null;
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    dtTemp = ds.Tables[0];
                }
            }
            return dtTemp;
        }
        /* 执行SQL语句，返回结果 */
        public OracleDataReader readerData(string sql)
        {
            connDb();
            comm = new OracleCommand(sql, conn);
            OracleDataReader dr = comm.ExecuteReader();
            return dr;
        }

        public OracleDataReader readerData(string sql, OracleConnection Connection)
        {
            conn = Connection;
            comm = new OracleCommand(sql, conn);
            OracleDataReader dr = comm.ExecuteReader();
            return dr;
        }

        /* 执行SQL语句，不返回结果 */
        public int exeSql(string sql)
        {
            connDb();
            comm = new OracleCommand(sql, conn);
            int pReturn = comm.ExecuteNonQuery();
            return pReturn;
        }

        public int exeSql(string sql, OracleConnection Connection)
        {
            conn = Connection;
            comm = new OracleCommand(sql, conn);
            int pReturn = comm.ExecuteNonQuery();
            return pReturn;
        }
        /* 关闭链接 */
        public void clear()
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
        }
    }
}